/*  
 * @(#) PageTagHelper.java Create on 2012-5-10 下午5:19:40   
 *   
 * Copyright 2012 by xl.   
 */

package cn.com.common.ui;

/**
 * 
 * @author zhanghongliang
 * @date 2012-5-10
 */
public class PageTagHelper {
	/* 默认显示几个 */
	public static final int DEFAULT_VIEWSIZE = 7;

	public static String getPageTag(int pageNow, int pageCount, int total, int viewPageSize) {
		if (viewPageSize == 0) {
			viewPageSize = DEFAULT_VIEWSIZE;
		}
		StringBuffer result = new StringBuffer();
		if (pageNow <= 1) {
			result.append("<span >首页</span>\r\n");
			result.append("<span >上一页</span>\r\n");
		} else {
			result.append("<a href='javascript:void(0);' onclick='goPage(1,this)'><span >首页</span></a>\r\n");
			result.append("<a href='javascript:void(0);' onclick='goPage(" + (pageNow - 1) + ",this)'><span >上一页</span></a>\r\n");
		}
		if (pageCount == 0) {
			result.append("<span >1</span>\r\n");
		} else {
			int start = 1;
			if (pageCount > viewPageSize && pageNow > 1) {
				int _tmpStart = pageNow - viewPageSize / 2;
				int _endPage = pageNow + viewPageSize / 2;
				if (_tmpStart > 1 && _endPage < pageCount) {
					start = _tmpStart;
				} else if (_tmpStart > 1) {
					start = _tmpStart;
				}
			}

			if (pageCount - start < viewPageSize) {
				start = start - (viewPageSize - (pageCount - start + 1));
			}
			if (start < 0) {
				start = 1;
			}

			for (int _i = 0; _i < viewPageSize; _i++) {
				if (start > pageCount) {
					break;
				}
				if (start == pageNow) {
					result.append("<span >" + start + "</span>\r\n");
				} else {
					result.append("<a href='javascript:void(0);' onclick='goPage(" + (start) + ",this)'><span >"+start+"</span></a>\r\n");
				}
				start++;
			}
		}
		if (pageNow == pageCount || pageCount <= 1) {
			result.append("<span >下一页</span>\r\n");
		} else {
			result.append("<a href='javascript:void(0);' onclick='goPage(" + (pageNow + 1) + ",this)'><span >下一页</span></a>\r\n");
		}

		return result.toString();
	}
}
